package com.serenegiant.common;

import android.app.Service;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import com.serenegiant.utils.HandlerThreadHandler;

public abstract class BaseService extends Service {
    private static boolean DEBUG = false;
    private static final String TAG = "BaseService";
    private final Handler mUIHandler;
    private final Thread mUiThread;
    private Handler mWorkerHandler;
    private long mWorkerThreadID;

    public BaseService() {
        Handler handler = new Handler(Looper.getMainLooper());
        this.mUIHandler = handler;
        this.mUiThread = handler.getLooper().getThread();
        this.mWorkerThreadID = -1L;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (this.mWorkerHandler == null) {
            HandlerThreadHandler createHandler = HandlerThreadHandler.createHandler(TAG);
            this.mWorkerHandler = createHandler;
            this.mWorkerThreadID = createHandler.getLooper().getThread().getId();
        }
    }

    @Override
    public synchronized void onDestroy() {
        if (this.mWorkerHandler != null) {
            try {
                this.mWorkerHandler.getLooper().quit();
            } catch (Exception unused) {
            }
            this.mWorkerHandler = null;
        }
        super.onDestroy();
    }

    public final void runOnUiThread(Runnable runnable, long j) {
        if (runnable == null) {
            return;
        }
        this.mUIHandler.removeCallbacks(runnable);
        if (j > 0 || Thread.currentThread() != this.mUiThread) {
            this.mUIHandler.postDelayed(runnable, j);
            return;
        }
        try {
            runnable.run();
        } catch (Exception e) {
            Log.w(TAG, e);
        }
    }

    public final void removeFromUiThread(Runnable runnable) {
        if (runnable == null) {
            return;
        }
        this.mUIHandler.removeCallbacks(runnable);
    }

    protected final synchronized void queueEvent(Runnable runnable, long j) {
        if (runnable != null)
        {
            Handler handler = this.mWorkerHandler;
            if (handler != null)
            {
                try {
                    handler.removeCallbacks(runnable);
                    if (j > 0)
                    {
                        this.mWorkerHandler.postDelayed(runnable, j);
                    } else if (this.mWorkerThreadID == Thread.currentThread().getId()) {
                        runnable.run();
                    } else {
                        this.mWorkerHandler.post(runnable);
                    }
                } catch (Exception unused) {
                }
            }
        }
    }

    protected final synchronized void removeEvent(Runnable runnable)
    {
        if (runnable == null) {
            return;
        }
        try {
            this.mWorkerHandler.removeCallbacks(runnable);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
